#!/bin/bash
#
# getdocker registry 脚本 2016-09-01
#
dockerRegistry=''
if [[ -f "/usr/local/etc/registry" ]]; then
	dockerRegistry=`cat /usr/local/etc/registry`
fi
if [[ ! -z "$dockerRegistry" ]]; then
	read -p "dockerRegistry is $dockerRegistry? y/n " confirm
	if [[ $confirm != 'y' ]]; then
		dockerRegistry=''
	fi

fi
while [[ -z $dockerRegistry ]]; do
        read -p "Please input the docker registry url [eg: 172.16.0.24:5000]: " dockerRegistry
        ip=`echo $dockerRegistry|awk -F ':' '{print $1}'`
        port=`echo $dockerRegistry|awk -F ':' '{print $2}'`
        if [ -z "$port" ];then
            echo "port is null, please try again."
            dockerRegistry=''
            continue
        fi
        if [[ $ip =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then
            echo "dockerRegistry is :$dockerRegistry"
            echo "$dockerRegistry" > /usr/local/etc/registry
        else
            echo "error input ip, please try again."
            dockerRegistry=''
        fi
done